
import os
import sys

def openInputStream(filename):
    """Open a readable file or unzipping process,
    depending on whether filename suffix is .gz."""

    if filename.endswith(".gz"):
        return os.popen("gunzip -c %s" % filename)
    return open(filename, "r")

def getFilePrefixes(dir, suffix, allowGzip=True):
    ret = []
    if not suffix.startswith("."):
        suffix = "." + suffix
    for filename in os.listdir(dir):
        if filename.endswith(suffix):
            ret.append(filename.replace(suffix, ""))
        elif allowGzip and filename.endswith(suffix + ".gz"):
            ret.append(filename.replace(suffix + ".gz", ""))
    return ret

def printUsage(args, usage, minArgs):
    if len(args) < minArgs or (len(args)>1 and (args[1].startswith("-h") or args[1].startswith("--h"))):
        print usage
        sys.exit(1)

